package cn.core.sap.modules.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.core.sap.dao.mapper.Sap_jcodatasourceMapper;
import cn.core.sap.dao.po.Sap_jcodatasourcePO;
import cn.osworks.aos.core.asset.AOSCons;
import cn.osworks.aos.core.asset.AOSUtils;
import cn.osworks.aos.core.id.AOSId;
import cn.osworks.aos.core.typewrap.Dto;
import cn.osworks.aos.core.typewrap.Dtos;

/**
 * <b>SAP数据源[sap_jcodatasource] controller</b>
 * <p>
 * </p>
 * 
 * @author penghe
 * @date 2016-08-17 15:45:23
 */
@Service
public class Sap_jcodatasourceService {
	@Autowired
	private Sap_jcodatasourceMapper sap_jcodatasourcemapper;
	
	/** 保存信息SAP数据源
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto saveSap_jcodatasource(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Sap_jcodatasourcePO sap_jcodatasourcePO = new Sap_jcodatasourcePO();
		AOSUtils.copyProperties(inDto, sap_jcodatasourcePO);
                                        	    if (!checkIsExist(sap_jcodatasourcePO)) {
			outDto.setAppCode(AOSCons.ERROR);
			String msg = sap_jcodatasourcePO.getPk_()+"已被存在, 请重新输入。";
			outDto.setAppMsg(msg);
			return outDto;
		}
 		sap_jcodatasourcePO.setId_(AOSId.uuid());
		sap_jcodatasourcemapper.insert(sap_jcodatasourcePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，新增成功。");
		return outDto;
	}
	/** 更新SAP数据源
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto updateSap_jcodatasource(Dto inDto) {
		Dto outDto = Dtos.newDto();
		Sap_jcodatasourcePO sap_jcodatasourcePO = new Sap_jcodatasourcePO();
		AOSUtils.copyProperties(inDto, sap_jcodatasourcePO);
		sap_jcodatasourcemapper.updateByKey(sap_jcodatasourcePO);
		outDto.setAppCode(AOSCons.SUCCESS);
		outDto.setAppMsg("操作完成，更新成功。");
		return outDto;
	}
	/** 删除SAP数据源
	 * @param inDto
	 * @return
	 */
	@Transactional
	public Dto delete(Dto qDto) {
		Dto outDto = Dtos.newDto();
		String[] selections = qDto.getRows();
		int del = 0;
		for (String id_ : selections) {
			sap_jcodatasourcemapper.deleteByKey(id_);
			del++;
		}
		String msg = "操作完成, ";
		if (del > 0) {
			msg = AOSUtils.merge(msg + "成功删除[{0}]个。", del);
		}
		outDto.setAppMsg(msg);
		return outDto;
	}
	/** 判断存在SAP数据源
	 */
	public boolean checkIsExist(Sap_jcodatasourcePO sap_jcodatasourcePO) {
		Dto calcDto = Dtos.newCalcDto("COUNT(id_)");
            calcDto.put("pk_", sap_jcodatasourcePO.getPk_()); 
                 		Integer countInteger = Integer.valueOf(sap_jcodatasourcemapper.calc(calcDto));
		boolean out = true;
		if (countInteger > 0) {
			out = false;
		}
		return out;
	}
	
}
